package cn.bjc.controller;

import cn.bjc.domain.User;
import cn.bjc.fallbanck.OrderFallback;
import cn.bjc.feign.UserFeignService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
    
    @Autowired
    private UserFeignService UserFeignService;

    
    @GetMapping("/getById/{id}")
    //在需要通过Sentinel来控制流量的地方使用@SentinelResource注解
    //@SentinelResource(value="order-getById", blockHandler = "blockMethod", fallback = "fallbackOrderMethod", fallbackClass = OrderFallback.class)
    public User getById(@PathVariable("id") Long id){
        return UserFeignService.getById(id);

    }

    //熔断的本类方法
    //public User fallbackOrderMethod(@PathVariable("id") Long id,Throwable e){
    //    e.printStackTrace();
    //    return new User(-1L, "触发本类熔断", "触发Sentinel对user服务熔断,服务暂时不可用!");
    //
    //}


    //限流的本类方法
    public User blockMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new User(-1L, "触发本类限流", "触发Sentinel限流,服务暂时不可用!");

    }
}
